package com.zhilifang.modules.zlf.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhilifang.modules.zlf.entity.IdType;
import com.zhilifang.modules.zlf.entity.Information;
import com.zhilifang.modules.zlf.entity.InformationType;
import com.zhilifang.modules.zlf.service.impl.InformationServiceImpl;
import com.zhilifang.modules.zlf.service.impl.InformationTypeServiceImpl;
import com.zhilifang.modules.zlf.utils.Pager;
import com.zhilifang.modules.zlf.utils.QueryString;
import com.zhilifang.modules.zlf.utils.Result;

/**
 * Controller 后台首页>>发布新闻Controller
 */
@Controller
@RequestMapping("/informationController")
public class InformationController {

	@Autowired
	private InformationServiceImpl informationServiceImpl ;
	
	@Autowired
	private InformationTypeServiceImpl informationTypeServiceImpl;

	
	
	/**
	 * 前端查询所有显示新闻类型
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selecInformationTypeListr", method = RequestMethod.POST)
	@ResponseBody
	public List<InformationType> selecInformationTypeListr(){
		List<InformationType> list =  informationTypeServiceImpl.selecInformationTypeListr();
		return list;
	}
	
	/**
	 * 首页展示查询所有新闻，根据类型id查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectInformationListr", method = RequestMethod.POST)
	@ResponseBody
	public Pager<Information> selectInformationListr(Integer id,Integer currentPage){
		Pager<Information> pager = new Pager<Information>();
		List<Information> list = informationServiceImpl.selectInformationListr(id);	
		if (list != null&&!list.isEmpty()) {
			pager=new Pager<Information>(currentPage,6, list);
		}
		return pager;
	}
	
	/**
	 * 产品内容列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selecInformationPageQueryString", method = RequestMethod.POST)
	@ResponseBody
	public Pager<Information> selecInformationPageQueryString(QueryString queryString){
		Pager<Information> pager = new Pager<Information>();
		if(!"".equals(queryString.getReady01())&&queryString.getReady01()!=null){
			queryString.setReady01("%"+queryString.getReady01()+"%");  //内容标题
		}
		if(!"".equals(queryString.getReady02())&&queryString.getReady02()!=null){
			queryString.setReady02(queryString.getReady02());  //产品分类
		}
		List<Information> list = informationServiceImpl.selecInformationPageQueryString(queryString);
		if (list != null&&!list.isEmpty()) {
			pager=new Pager<Information>(queryString.getCurrentPage(),queryString.getPageSize(), list);
		}
		return pager;
	}
	
	
	/**
	 * 删除&批量删除新闻
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteInformationIds", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteInformationIds(int [] ids){
		Result result = new Result();
		informationServiceImpl.deleteInformationIds(ids);	
		result.setMessage("删除成功");
		result.setState("200");
		return result;
	}
	
	/**
	 * 查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectInformationById", method = RequestMethod.POST)
	@ResponseBody
	public Information selectProductById(int  id){
		Information  result = informationServiceImpl.selectInformationById(id);
		return result;
	}

	/**
	 * 发布新闻&&修改新闻
	 * 
	 * @return
	 */
	@RequestMapping(value = "/insertUpdateInformation", method = RequestMethod.POST)
	@ResponseBody
	public Result insertUpdateInformation(Information information){
		Result result = new Result();
		if(information.getId()!=null){
			informationServiceImpl.updateInformation(information);
			result.setMessage("修改成功");
			result.setState("200");
		}else{
			informationServiceImpl.insertInformation(information);
			result.setMessage("添加成功");
			result.setState("200");
		}
		return result;
	}
	
	/**
	 * 删除&批量删除新闻类型
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteIndustryTypeIds", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteIndustryTypeIds(int [] ids){
		Result result = new Result();
		informationTypeServiceImpl.deleteIndustryTypeIds(ids);	
		result.setMessage("删除成功");
		result.setState("200");
		return result;
	}
	
	
	/**
	 * 根据当前id和类型查询上一条和下一条的信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selecUpInDn", method = RequestMethod.POST)
	@ResponseBody
	public List<Information> selecUpInDn(Integer typeId,Integer id){
		List<Information> list = informationServiceImpl.selecUpInDn(typeId, id);	
		return list;
	}

	/**
	 * 根据类型查询当前最小id和最大id
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selecMinAndMaxId", method = RequestMethod.POST)
	@ResponseBody
	public IdType selecMinAndMaxId(Integer id){
		IdType list = informationServiceImpl.selecMinAndMaxId(id);	
		return list;
	}
	
	/**
	 * 新增类型&&修改类型
	 * 
	 * @return
	 */
	@RequestMapping(value = "/insertUpdateInformationType", method = RequestMethod.POST)
	@ResponseBody
	public Result insertUpdateInformationType(InformationType informationType){
		Result result = new Result();
		if(informationType.getId()!=null){
			informationTypeServiceImpl.updateInformationType(informationType);
			result.setMessage("修改成功");
			result.setState("200");
		}else{
			informationTypeServiceImpl.insertInformationType(informationType);
			result.setMessage("添加成功");
			result.setState("200");
		}
		return result;
	}
	
	/**
	 * 类型查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectTypeById", method = RequestMethod.POST)
	@ResponseBody
	public InformationType selectTypeById(int  id){
		InformationType  result = informationTypeServiceImpl.selecInformationTypeById(id);
		return result;
	}
	
	/**
	 * 后台查询所有显示新闻类型
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectInformationTypeIndexListr", method = RequestMethod.POST)
	@ResponseBody
	public List<InformationType> selectInformationTypeIndexListr(){
		List<InformationType> list =  informationTypeServiceImpl.selectInformationTypeIndexListr();
		return list;
	}

}
